package com.ruoyi.platform.system.file.controller;

import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.base.utils.number.NumberUtils;
import com.ruoyi.platform.system.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传
 *
 * @author lcf
 */
@Api(tags = {"文件上传"})
@RequestMapping("/file")
@RestController
public class FileUploadController extends BaseController {
    private final FileUploadService uploadService;

    public FileUploadController(FileUploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * 上传图片
     */
    @ApiOperation("上传图片")
    @PostMapping("/img")
    public HttpResult<String> uploadImg(@ApiParam(value = "图片", required = true) MultipartFile file,
                                        @ApiParam(value = "上传路径", required = true) String path,
                                        @ApiParam("是否压缩（1是0否，默认0）") Integer isCompress) {
        InputCheckUtils.checkNull(file, "请选择文件");
        InputCheckUtils.checkNull(path, "上传路径为空");

        return data(uploadService.uploadImg(path, file, NumberUtils.isNotEmpty(isCompress)));
    }

    /**
     * 多图片上传
     */
    @ApiOperation("多图片上传")
    @PostMapping("/img/list")
    public HttpResult<List<String>> uploadImg(@ApiParam(value = "图片", required = true) MultipartFile[] file,
                                              @ApiParam(value = "上传路径", required = true) String path,
                                              @ApiParam("是否压缩（1是0否，默认0）") Integer isCompress) {
        InputCheckUtils.checkArray(file, "请选择文件");
        InputCheckUtils.checkNull(path, "上传路径为空");

        return data(uploadService.uploadImg(path, file, NumberUtils.isNotEmpty(isCompress)));
    }

    /**
     * 上传文件
     */
    @ApiOperation("上传文件")
    @PostMapping("/file")
    public HttpResult<String> uploadFile(@ApiParam(value = "文件", required = true) MultipartFile file,
                                         @ApiParam(value = "上传路径", required = true) String path) {
        InputCheckUtils.checkNull(file, "请选择文件");
        InputCheckUtils.checkNull(path, "上传路径为空");

        return data(uploadService.uploadFile(path, file));
    }

    /**
     * 多文件上传
     */
    @ApiOperation("多文件上传")
    @PostMapping("/file/list")
    public HttpResult<List<String>> uploadFile(@ApiParam(value = "文件", required = true) MultipartFile[] file,
                                               @ApiParam(value = "上传路径", required = true) String path) {
        InputCheckUtils.checkArray(file, "请选择文件");
        InputCheckUtils.checkNull(path, "上传路径为空");

        return data(uploadService.uploadFile(path, file));
    }
}
